package com.sinux.generality.basesupport.utils.file;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.sinux.generality.basesupport.utils.tool.StringTool;

/**
 * 文件中心
 * @ClassName CenterConfig
 * @Description: 获取配置的地方
 * @author Tangjc
 * @date 2021年5月6日 下午3:18:32
 */
public class CenterConf {
    /** conf.properties */
    private static Map<String, Object> centerMap = new HashMap<String, Object>();
	/**
	 * 
	 * @Title: get
	 * @Description: 通用返回方法
	 * @author Tangjc
	 * @param extension
	 * @param cla
	 * @return
	 * @throws Exception
	 * @return T
	 */
	public static <T> T get(String extension, Class<T> cla) throws Exception{
		if(centerMap.containsKey(extension)){
			return (T) centerMap.get(extension);
		}
		
		// 根据extension找到对应解析方法
		String qualifiledName = CenterConf.class.getCanonicalName();
		String allClassName = qualifiledName.replaceAll("Center", StringTool.upperFirst(extension));
		System.out.println(allClassName);
		
		// 返回不同类型
		Class Conf = Class.forName(allClassName);
		Constructor constructor = Conf.getConstructor(String.class);
		BaseConf bc = (BaseConf)constructor.newInstance(extension);
//		BaseConf bc = (BaseConf)Conf.newInstance();
		centerMap.put(extension, bc.getParamsMap(cla));
		return bc.getParamsMap(cla);
	}
	
	public static Map<String, String> getProperties() throws Exception{
		return get("properties", Map.class);
	}
	
	public static Map<String, Map<String, Object>> getXmls() throws Exception{
		return get("xml", Map.class);
	}
	
	public static JSONObject getJsons() throws Exception{
		return get("json", JSONObject.class);
	}
}
